import { Request, toast } from '../../../utils/util.js'
let request = new Request()

Page({
  data: {
    locationShow : "",

    type: 0,
    amount : '',
    quantity : '',
    content : '',
    location: '',
    longitude: '',
    latitude: '',
    images: [],

    cityId : 0
  },

  input(e) {
    let target = e.currentTarget.dataset.target
    let update = {}
    update[target] = e.detail.value
    this.setData(update)
  },

  addImage() {
    let images = this.data.images
    let d = new Date()
    images.push({ file: '', id: d.getTime() })
    this.setData({ images: images })
  },

  uploadPic(e) {
    let file = e.detail.value
    let index = parseInt(e.target.dataset.index)
    let update = {}
    update[`images[${index}].file`] = file
    if (index < 8) {
      let d = new Date()
      update[`images[${index + 1}]`] = { file: '', id: d.getTime() }
    }
    this.setData(update)
  },

  clearPic(e) {
    let index = parseInt(e.target.dataset.index)
    let images = this.data.images
    if (images.length == 1) {
      images[0] = ''
    } else {
      images.splice(index, 1)
    }

    this.setData({ images: images })
  },

  selectedLocation (e) {
    let type = e.currentTarget.dataset.type
    
    if(type == 1){ //附近位置
      wx.chooseLocation({
        success: res => {
          this.setData({ location: res.name, longitude: res.longitude, latitude: res.latitude, type : type})
        }
      })
    }else if(type == 2){
      wx.navigateTo({
        url: '/pages/area/selectCity/index'
      })
    }else{
      this.setData({ type: type, location : "全国范围" })
    }

  },

  selectCity (e) {
    this.setData({ cityId: e.id, location: e.name, type : 2})
  },

  confirm () {
    let amount = this.data.amount
    if (amount === '') {
      toast('请输入总金额')
      return
    }

    if(amount <= 0){
      toast('金额不能少于0.01')
      return
    }

    let quantity = this.data.quantity
    if (quantity === '') {
      toast('请输入红包数量')
      return
    }
    if (quantity < 1){
      toast('红包数量不能小于1')
      return
    }
    let type = this.data.type
    if (type === 0) {
      toast('请选择发布范围')
      return
    }
    let data = {
      chatType : 3,
      type: type,
      amount: amount,
      quantity: quantity,
      content: this.data.content,
      picture : []
    }

    if(data.type == 1){
      data.location = this.data.location
      data.longitude = this.data.longitude
      data.latitude = this.data.latitude
    }else if(data.type == 2){
      data.cityId = this.data.cityId
    }

    let images = this.data.images
    images.forEach(v => {
      if (v.file !== '') {
        data.picture.push(v.file)
      }
    })

    request.post('publish', res => {
      if(res.success){
        let params = res.data.wxparam
        params.success = () => {
          toast('发布成功')
          wx.navigateBack({
            delta: 3
          })
        }

        params.fail = () => {
          toast('支付失败')
        }

        wx.requestPayment(params)
      }else{
        toast(res.msg)
      }
    }, data)
  },

  onLoad: function (options) {
    this.addImage()

    wx.setNavigationBarTitle({
      title: '发布现金红包'
    })
  }
})